import { getCategoryInitData, getGoodList } from '@/api/index'

const actions = {
    async getCategoryInitData({ commit }) {
        const result = await getCategoryInitData()
        if(result.errno != 0) return Promise.reject(result.errmsg)
        commit('INITDATA', result.data)
        return '成功'
    },
    // 获取当前id的商品列表
    async getCurrentGoodList({ commit }, id) {
        const result = await getGoodList(id)
        commit('CURRENTGOODLIST', result.data)
    }
}
const mutations = {
    INITDATA(state, data) {
        state.imageUrlAndTitleName = data.currentCategory
        state.navList = data.categoryList
        // console.log(data.categoryList)
        state.currentList = data.currentCategory.subCategoryList
    },
    CURRENTGOODLIST(state, data) {
        state.currentList = []
        state.currentList = data.currentCategory.subCategoryList
        state.imageUrlAndTitleName = data.currentCategory        
    }
}
const state = {
    currentList: [],
    navList: [],
    imageUrlAndTitleName: []
}
const getters = {
    // 导航栏数据
    navList(state) {
        return state.navList || []
    },
    currentGoodList(state) {
        return state.currentList || []
    },
    imageUrl(state) {
        return state.imageUrlAndTitleName.banner_url || ''
    },
    titleName(state) {
        return state.imageUrlAndTitleName.name || ''
    }

}

export default {
    actions,
    mutations,
    state,
    getters
}